package com.lipaluma.example.clients.test.service;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

import com.lipaluma.example.clients.model.Invoice;
import com.lipaluma.example.clients.service.InvoiceService;
import com.lipaluma.test.db.SpringJUnit4WithDBClassRunner;
import com.lipaluma.test.db.annotations.LoadDataInfile;
import com.lipaluma.test.db.annotations.assertions.AssertColumns;
import com.lipaluma.test.db.annotations.batch.Batch;
import com.lipaluma.test.db.annotations.clean.CleanSchema;
import com.lipaluma.test.db.annotations.dataSource.Dialect;
import com.lipaluma.test.db.internal.DialectEnum;

@RunWith(SpringJUnit4WithDBClassRunner.class)
@Dialect(DialectEnum.H2)
@ContextConfiguration("classpath:applicationContext-service.xml")
public class InvoiceServiceTestIntegration {

	@Autowired
	private InvoiceService invoiceService;
	
	/**
	 * Test That check the number of invoices created in the database and check the size of the returned list
	 */
	@CleanSchema
	@Batch("classpath:sql/DataComplete-generationInvoices.sql")
	@AssertColumns(expected="18", request="select count(*) from t_invoice")
	@Test
	public void testGenerateInvoices() {
		List<Invoice> invoices = invoiceService.generateInvoices();
		assertEquals(18, invoices.size());
	}

	/**
	 * Same test than before except the data loading is done by the {@link LoadDataInfile}.
	 * The same data is injected
	 */
	@CleanSchema
	@LoadDataInfile(value="classpath:sql/data-complete", fieldDelim=",", headers=true)
	@AssertColumns(expected="18", request="select count(*) from t_invoice")
	@Test
	public void testGenerateInvoices2() {
		List<Invoice> invoices = invoiceService.generateInvoices();
		assertEquals(18, invoices.size());
	}

}
